#include "../echo_service.pb.h"
#include "../RpcServer.h"
#include "../../EventLoop.h"

class EchoServerImpl : public EchoServer
{
public:

    virtual void Echo(::google::protobuf::RpcController* controller,
                      const EchoRequest* request,
                      EchoResponse* response,
                      ::google::protobuf::Closure* done) override
    {
        response->set_message("echo message: " + request->message());
        done->Run();
    }

};

int main (int argc, char* argv[])
{
    if (argc < 2)
    {
        fprintf(stderr, "Usage: %s listen_port backend_ip:port [backend_ip:port]\n", argv[0]);
        exit(0);
    }
    int port = atoi(argv[1]);
    std::cout << "listen port : " << port << std::endl;

    int numThreads = 4;

    EventLoop mainLoop;
    
    RpcServer rpc_server(&mainLoop, port);
    rpc_server.setThreadNum(numThreads);

    EchoServer* echo_service = new EchoServerImpl();

    rpc_server.registerService(echo_service);

    rpc_server.start();

    mainLoop.loop();

    return 0;
}

// g++ -std=c++17 main.cpp ../*.cpp ../../*.cpp -lpthread -lprotobuf -o server